package com.amc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.amc.dao.IMerchantDao;
import com.amc.dto.FoodView;
import com.amc.entity.Comment;
import com.amc.entity.Food;
import com.amc.entity.FoodType;
import com.amc.entity.Merchant;

@Repository
public class MerchantDaoImpl implements IMerchantDao {
	@PersistenceContext
	private EntityManager em;

	public void setEm(EntityManager em) {
		this.em = em;
	}

	@Override
	public List<Merchant> queryAllMerchants(List<String> merchantsId) {
		List<Merchant> merchants = new ArrayList<Merchant>();
		Merchant m = null;
		for (String merchantId : merchantsId) {
			Query query = em
					.createQuery(
							"select m from Merchant m, Food f where m.merchantId = f.foodMerchant.merchantId and m.merchantId = ?")
					.setParameter(1, merchantId);
			try {
				m = (Merchant) query.getSingleResult();
				merchants.add(m);
			} catch (Exception e) {
				System.out.println(merchantId + "此商家没过审");
				// e.printStackTrace();
			}
		}
		return merchants;
	}

	@Override
	public List<Merchant> queryMerchantByFoodType(String foodType) {
		Query query = em
				.createQuery(
						"select distinct m from Merchant m, Food f, FoodType ft where m.merchantId=f.foodMerchant.merchantId and f.foodType.typeId=?")
				.setParameter(1, foodType);
		List<Merchant> merchants = new ArrayList<>();
		try {
			merchants = query.getResultList();
		} catch (Exception e) {
			System.out.println("从菜品下拉列表获取商家列表失败");
		}
		return merchants;
	}

	@Override
	public List<Merchant> queryMerchants(String keyWord) {
		Query query = em
				.createQuery("select m from Merchant m where m.merchantName like  '%"
						+ keyWord + "%'");
		List<Merchant> merchants = null;
		try {
			merchants = query.getResultList();
		} catch (Exception e) {
			System.out.println("商家查询错误");
		}

		return merchants;
	}

	@Override
	public List<FoodType> queryFoodTypeByMerchantList(List<Merchant> merchants) {
		List<FoodType> foodTypes = new ArrayList<>();
		List<FoodType> temp = new ArrayList<>();
		for (Merchant merchant : merchants) {

			Query query = em.createQuery(
					"select ft from Food f join  f.foodType ft  where  "
							+ "f.foodMerchant in (select m from Food f join  "
							+ " f.foodMerchant m where m.merchantId=?) ")
					.setParameter(1, merchant.getMerchantId());
			try {
				temp = query.getResultList();
			} catch (Exception e) {
				System.out.println("商家菜品下拉框初始化失败");
			}
			for (FoodType ft : temp) {
				if (!foodTypes.contains(ft)) {
					foodTypes.add(ft);
				}
			}
		}
		return foodTypes;
	}

	@Override
	public List<FoodView> queryFood(List<String> advId) {
		List<Food> foods = new ArrayList<>();
		List<FoodView> foodview = new ArrayList<>();
		for (String foodId : advId) {
			foods.add(em.find(Food.class, foodId));
		}

		if (foods.size() != 0 && foods != null) {
			for (Food food : foods) {
				if (food != null) {
					foodview.add(new FoodView(null, food.getFoodId(), food
							.getFoodName(), food.getFoodPrice(), food
							.getFoodPic(), food.getFoodType().getTypeId(), 1));
				}
			}
		}
		return foodview;
	}

	@Override
	public double calculateSore(String merchantId) {
		double sorce = 0;
		Query query=em.createQuery("select c from Comment c where c.merchant.merchantId=?").setParameter(1, merchantId);// where c.merchant.merchantId=?//.setParameter(1, merchantId)
		try{
		List<Comment> msorce=query.getResultList();
		// System.out.println(msorce.size());
		if(msorce!=null&&msorce.size()!=0)
		{
		int size=msorce.size();
		for(Comment temp:msorce){
		sorce+=temp.getCommentScore();
		}
		sorce=sorce/size;
		}
		}catch(Exception e){
		System.out.println("查询失败");
		}
		return sorce;
	}
}
